#include "lcd_display.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"

// LCD控制引脚定义
#define LCD_SI_PIN    GPIO_Pin_12
#define LCD_SCL_PIN   GPIO_Pin_13
#define LCD_A0_PIN    GPIO_Pin_14
#define LCD_RES_PIN   GPIO_Pin_15
#define LCD_CS1_PIN   GPIO_Pin_11
#define LCD_BLA_PIN   GPIO_Pin_10
#define LCD_GPIO_PORT GPIOB
#define LCD_GPIO_CLK  RCC_APB2Periph_GPIOB

// 8×16字模数据（数字和符号）
unsigned char zimo_8x16[][16] = {
    // 0 (8×16)
    {0x00,0x3C,0x42,0x42,0x42,0x42,0x3C,0x00,
     0x00,0x18,0x24,0x24,0x24,0x24,0x18,0x00},
    // 1 (8×16)
    {0x00,0x00,0x22,0x7E,0x22,0x00,0x00,0x00,
     0x00,0x00,0x12,0x3E,0x12,0x00,0x00,0x00},
    // 2 (8×16)
    {0x00,0x3C,0x42,0x02,0x04,0x48,0x7E,0x00,
     0x00,0x18,0x24,0x04,0x08,0x20,0x3E,0x00},
    // 3 (8×16)
    {0x00,0x3C,0x42,0x02,0x1C,0x42,0x3C,0x00,
     0x00,0x18,0x24,0x04,0x18,0x24,0x18,0x00},
    // 4 (8×16)
    {0x00,0x08,0x18,0x28,0x7E,0x08,0x08,0x00,
     0x00,0x04,0x04,0x04,0x3E,0x04,0x04,0x00},
    // 5 (8×16)
    {0x00,0x7E,0x40,0x7C,0x02,0x42,0x3C,0x00,
     0x00,0x3E,0x20,0x3C,0x04,0x24,0x18,0x00},
    // 6 (8×16)
    {0x00,0x3C,0x40,0x7C,0x42,0x42,0x3C,0x00,
     0x00,0x18,0x20,0x3C,0x24,0x24,0x18,0x00},
    // 7 (8×16)
    {0x00,0x7E,0x42,0x04,0x08,0x10,0x10,0x00,
     0x00,0x3E,0x24,0x04,0x08,0x10,0x10,0x00},
    // 8 (8×16)
    {0x00,0x3C,0x42,0x3C,0x42,0x42,0x3C,0x00,
     0x00,0x18,0x24,0x18,0x24,0x24,0x18,0x00},
    // 9 (8×16)
    {0x00,0x3C,0x42,0x42,0x3E,0x02,0x3C,0x00,
     0x00,0x18,0x24,0x24,0x1E,0x04,0x18,0x00},
    // ： (冒号)
    {0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,
     0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00},
    // . (小数点)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00},
    // V (电压单位)
    {0x00,0x42,0x24,0x18,0x18,0x24,0x42,0x00,
     0x00,0x22,0x14,0x08,0x08,0x14,0x22,0x00},
    // A (电流单位)
    {0x00,0x7C,0x12,0x11,0x12,0x7C,0x00,0x00,
     0x00,0x3F,0x20,0x20,0x20,0x3F,0x00,0x00},
    // m (毫单位)
    {0x00,0x42,0x62,0x52,0x4A,0x46,0x42,0x00,
     0x00,0x22,0x32,0x2A,0x26,0x22,0x22,0x00},
    // - (横线)
    {0x00,0x00,0x00,0x7E,0x7E,0x00,0x00,0x00,
     0x00,0x00,0x00,0x3E,0x3E,0x00,0x00,0x00},
    // U
    {0x00,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,
     0x00,0x22,0x22,0x22,0x22,0x18,0x00,0x00},
    // D
    {0x00,0x82,0x42,0x42,0x42,0x3C,0x00,0x00,
     0x00,0x42,0x22,0x22,0x22,0x18,0x00,0x00},
    // S
    {0x00,0x3C,0x40,0x38,0x04,0x44,0x38,0x00,
     0x00,0x18,0x20,0x1C,0x08,0x24,0x18,0x00},
    // A (字符)
    {0x00,0x7C,0x12,0x11,0x12,0x7C,0x00,0x00,
     0x00,0x3F,0x20,0x20,0x20,0x3F,0x00,0x00},
    // V (字符)
    {0x00,0x42,0x24,0x18,0x18,0x24,0x42,0x00,
     0x00,0x22,0x14,0x08,0x08,0x14,0x22,0x00}
};

// 16×16汉字字模数据
unsigned char zimo_16x16[][32] = {
    // 地 (16×16)
    {0x00,0x00,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x10,0x00,0x00,
     0x00,0x00,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x1F,0x00},
    // 址 (16×16)
    {0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x10,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x1F,0x00},
    // 电 (16×16)
    {0x00,0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0x48,0xFF,0x48,0x48,0x48,0xC8,0x00,0x00,
     0x00,0x00,0x07,0x08,0x10,0x10,0x10,0x10,0x10,0x3F,0x10,0x10,0x10,0x10,0x0E,0x00},
    // 压 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00,0x00,
     0x00,0x00,0x1F,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0x00},
    // 池 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
    // 维 (16×16)
    {0x00,0x00,0x00,0xF0,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00,
     0x00,0x00,0x0F,0x10,0x10,0x10,0x10,0x3F,0x10,0x10,0x10,0x10,0x10,0x0F,0x00,0x00},
    // 护 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0xFF,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x3F,0x00,0x00,0x00},
    // 放 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00},
    // 否 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,0x00,0x00,0x00},
    // 供 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00},
    // 状 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 态 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 流 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 容 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 量 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 版 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 本 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 通 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 信 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 断 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 周 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 期 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 本 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 安 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 输 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // 出 (16×16)
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};

// 内部函数声明
static void LCD_GPIO_Init(void);
static void LCD_WriteCmd(uint8_t cmd);
static void LCD_WriteData(uint8_t data);
static void LCD_DisplayPage1(const LcdDynamicData *data);
static void LCD_DisplayPage2(const LcdDynamicData *data);
static void LCD_DisplayPage3(const LcdDynamicData *data);

// LCD GPIO初始化
static void LCD_GPIO_Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(LCD_GPIO_CLK, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin = LCD_SI_PIN | LCD_SCL_PIN | LCD_A0_PIN | LCD_RES_PIN | LCD_CS1_PIN | LCD_BLA_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LCD_GPIO_PORT, &GPIO_InitStructure);
    
    // 初始状态设置
    GPIO_SetBits(LCD_GPIO_PORT, LCD_CS1_PIN | LCD_BLA_PIN);
    GPIO_ResetBits(LCD_GPIO_PORT, LCD_SCL_PIN);
}

// 写命令
static void LCD_WriteCmd(uint8_t cmd) {
    uint8_t i;
    GPIO_ResetBits(LCD_GPIO_PORT, LCD_CS1_PIN);
    GPIO_ResetBits(LCD_GPIO_PORT, LCD_A0_PIN);  // 命令模式
    
    for(i = 0; i < 8; i++) {
        GPIO_ResetBits(LCD_GPIO_PORT, LCD_SCL_PIN);
        (cmd & 0x80) ? GPIO_SetBits(LCD_GPIO_PORT, LCD_SI_PIN) : GPIO_ResetBits(LCD_GPIO_PORT, LCD_SI_PIN);
        GPIO_SetBits(LCD_GPIO_PORT, LCD_SCL_PIN);
        cmd <<= 1;
    }
    
    GPIO_SetBits(LCD_GPIO_PORT, LCD_CS1_PIN);
}

// 写数据
static void LCD_WriteData(uint8_t data) {
    uint8_t i;
    GPIO_ResetBits(LCD_GPIO_PORT, LCD_CS1_PIN);
    GPIO_SetBits(LCD_GPIO_PORT, LCD_A0_PIN);  // 数据模式
    
    for(i = 0; i < 8; i++) {
        GPIO_ResetBits(LCD_GPIO_PORT, LCD_SCL_PIN);
        (data & 0x80) ? GPIO_SetBits(LCD_GPIO_PORT, LCD_SI_PIN) : GPIO_ResetBits(LCD_GPIO_PORT, LCD_SI_PIN);
        GPIO_SetBits(LCD_GPIO_PORT, LCD_SCL_PIN);
        data <<= 1;
    }
    
    GPIO_SetBits(LCD_GPIO_PORT, LCD_CS1_PIN);
}

// LCD初始化
void LCD_Init(void) {
    LCD_GPIO_Init();
    
    // 硬件复位
    GPIO_ResetBits(LCD_GPIO_PORT, LCD_RES_PIN);
    for(volatile uint32_t i = 0; i < 10000; i++);  // 简单延时
    GPIO_SetBits(LCD_GPIO_PORT, LCD_RES_PIN);
    
    // 初始化序列
    LCD_WriteCmd(0xAE);        // 显示关
    LCD_WriteCmd(0x40);        // 显示起始行=0
    LCD_WriteCmd(0xA0);        // ADC=0
    LCD_WriteCmd(0xC8);        // SHL=1
    LCD_WriteCmd(0xA2);        // LCD偏压=1/9
    LCD_WriteCmd(0x2F);        // 电源控制
    LCD_WriteCmd(0x81);        // 对比度调整
    LCD_WriteCmd(0x10);        // 对比度值
    LCD_WriteCmd(0xAF);        // 显示开
    
    LCD_Clear();
}

// 清屏
void LCD_Clear(void) {
    uint8_t page, col;
    for(page = 0; page < 8; page++) {
        LCD_WriteCmd(0xB0 + page);  // 设置页地址
        LCD_WriteCmd(0x00);         // 设置列地址低4位
        LCD_WriteCmd(0x10);         // 设置列地址高4位
        
        for(col = 0; col < 128; col++) {
            LCD_WriteData(0x00);    // 写入0，清除显示
        }
    }
}

// 显示8×16字符
void LCD_DisplayChar_8x16(uint8_t page, uint8_t col, Char8x16Index index) {
    uint8_t i;
    
    // 显示上8行
    LCD_WriteCmd(0xB0 + page);
    LCD_WriteCmd(0x00 + (col & 0x0F));
    LCD_WriteCmd(0x10 + ((col >> 4) & 0x0F));
    for(i = 0; i < 8; i++) {
        LCD_WriteData(zimo_8x16[index][i]);
    }
    
    // 显示下8行
    LCD_WriteCmd(0xB0 + page + 1);
    LCD_WriteCmd(0x00 + (col & 0x0F));
    LCD_WriteCmd(0x10 + ((col >> 4) & 0x0F));
    for(i = 8; i < 16; i++) {
        LCD_WriteData(zimo_8x16[index][i]);
    }
}

// 显示16×16汉字
void LCD_DisplayChar_16x16(uint8_t page, uint8_t col, Char16x16Index index) {
    uint8_t i;
    
    // 上8行左半部分
    LCD_WriteCmd(0xB0 + page);
    LCD_WriteCmd(0x00 + (col & 0x0F));
    LCD_WriteCmd(0x10 + ((col >> 4) & 0x0F));
    for(i = 0; i < 16; i++) {
        LCD_WriteData(zimo_16x16[index][i*2]);
    }
    
    // 上8行右半部分
    LCD_WriteCmd(0xB0 + page);
    LCD_WriteCmd(0x00 + ((col + 8) & 0x0F));
    LCD_WriteCmd(0x10 + (((col + 8) >> 4) & 0x0F));
    for(i = 0; i < 16; i++) {
        LCD_WriteData(zimo_16x16[index][i*2 + 1]);
    }
    
    // 下8行左半部分
    LCD_WriteCmd(0xB0 + page + 1);
    LCD_WriteCmd(0x00 + (col & 0x0F));
    LCD_WriteCmd(0x10 + ((col >> 4) & 0x0F));
    for(i = 16; i < 32; i++) {
        LCD_WriteData(zimo_16x16[index][i*2]);
    }
    
    // 下8行右半部分
    LCD_WriteCmd(0xB0 + page + 1);
    LCD_WriteCmd(0x00 + ((col + 8) & 0x0F));
    LCD_WriteCmd(0x10 + (((col + 8) >> 4) & 0x0F));
    for(i = 16; i < 32; i++) {
        LCD_WriteData(zimo_16x16[index][i*2 + 1]);
    }
}

// 显示数字（支持小数）
void LCD_DisplayNumber_8x16(uint8_t page, uint8_t col, float number, uint8_t intDigits, uint8_t decDigits) {
    int32_t integerPart, decimalPart;
    uint8_t i, digit;
    uint8_t currentCol = col;
    
    // 处理负数
    if(number < 0) {
        LCD_DisplayChar_8x16(page, currentCol, INDEX_HENG);
        currentCol += 8;
        number = -number;
    }
    
    // 分离整数和小数部分
    integerPart = (int32_t)number;
    decimalPart = (int32_t)((number - integerPart) * pow(10, decDigits) + 0.5);
    
    // 显示整数部分（右对齐）
    for(i = intDigits; i > 0; i--) {
        digit = (integerPart / (int)pow(10, i - 1)) % 10;
        LCD_DisplayChar_8x16(page, currentCol + (i - 1) * 8, (Char8x16Index)(INDEX_NUM0 + digit));
    }
    currentCol += intDigits * 8;
    
    // 显示小数点
    if(decDigits > 0) {
        LCD_DisplayChar_8x16(page, currentCol, INDEX_DIAN);
        currentCol += 8;
    }
    
    // 显示小数部分
    for(i = decDigits; i > 0; i--) {
        digit = (decimalPart / (int)pow(10, i - 1)) % 10;
        LCD_DisplayChar_8x16(page, currentCol + (i - 1) * 8, (Char8x16Index)(INDEX_NUM0 + digit));
    }
}

// 显示第一页
static void LCD_DisplayPage1(const LcdDynamicData *data) {
    LCD_Clear();
    
    // 第一行：地址:XX
    LCD_DisplayChar_16x16(0, 0, INDEX_HAN_DI);    // 地
    LCD_DisplayChar_16x16(0, 16, INDEX_HAN_ZHI);   // 址
    LCD_DisplayChar_8x16(0, 32, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(0, 40, data->address, 2, 0);  // 地址数值
    
    // 第二行：电池电压:XX.XV
    LCD_DisplayChar_16x16(2, 0, INDEX_HAN_DIAN);   // 电
    LCD_DisplayChar_16x16(2, 16, INDEX_HAN_CHI);   // 池
    LCD_DisplayChar_16x16(2, 32, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(2, 48, INDEX_HAN_YA);    // 压
    LCD_DisplayChar_8x16(2, 64, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(2, 72, data->batteryVoltage, 2, 1);  // 电压值
    LCD_DisplayChar_8x16(2, 72 + 3*8, INDEX_V);    // V
    
    // 第三行：维护放电:是/否
    LCD_DisplayChar_16x16(4, 0, INDEX_HAN_WEI);    // 维
    LCD_DisplayChar_16x16(4, 16, INDEX_HAN_HU);    // 护
    LCD_DisplayChar_16x16(4, 32, INDEX_HAN_FANG);  // 放
    LCD_DisplayChar_16x16(4, 48, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_8x16(4, 64, INDEX_MAO);        // ：
    LCD_DisplayChar_16x16(4, 72, data->isMaintenanceDischarge ? INDEX_HAN_FOU + 1 : INDEX_HAN_FOU);  // 是/否
    
    // 第四行：供电状态:正常/异常
    LCD_DisplayChar_16x16(6, 0, INDEX_HAN_GONG);   // 供
    LCD_DisplayChar_16x16(6, 16, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(6, 32, INDEX_HAN_ZHUANG); // 状
    LCD_DisplayChar_16x16(6, 48, INDEX_HAN_TAI);   // 态
    LCD_DisplayChar_8x16(6, 64, INDEX_MAO);        // ：
    if(data->powerSupplyStatus) {
        LCD_DisplayChar_16x16(6, 72, INDEX_HAN_DIAN);  // 正(假设用"电"占位)
        LCD_DisplayChar_16x16(6, 88, INDEX_HAN_CHI);  // 常(假设用"池"占位)
    } else {
        LCD_DisplayChar_16x16(6, 72, INDEX_HAN_WEI);  // 异(假设用"维"占位)
        LCD_DisplayChar_16x16(6, 88, INDEX_HAN_HU);  // 常(假设用"护"占位)
    }
}

// 显示第二页
static void LCD_DisplayPage2(const LcdDynamicData *data) {
    LCD_Clear();
    
    // 第一行：放电电流:XXXmA
    LCD_DisplayChar_16x16(0, 0, INDEX_HAN_FANG);   // 放
    LCD_DisplayChar_16x16(0, 16, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(0, 32, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(0, 48, INDEX_HAN_LIU);   // 流
    LCD_DisplayChar_8x16(0, 64, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(0, 72, data->dischargeCurrent, 3, 0);  // 电流值
    LCD_DisplayChar_8x16(0, 72 + 3*8, INDEX_m);    // m
    LCD_DisplayChar_8x16(0, 72 + 4*8, INDEX_A);    // A
    
    // 第二行：电池容量:SAV XXX
    LCD_DisplayChar_16x16(2, 0, INDEX_HAN_DIAN);   // 电
    LCD_DisplayChar_16x16(2, 16, INDEX_HAN_CHI);   // 池
    LCD_DisplayChar_16x16(2, 32, INDEX_HAN_RONG);  // 容
    LCD_DisplayChar_16x16(2, 48, INDEX_HAN_LIANG); // 量
    LCD_DisplayChar_8x16(2, 64, INDEX_MAO);        // ：
    LCD_DisplayChar_8x16(2, 72, INDEX_S);          // S
    LCD_DisplayChar_8x16(2, 80, INDEX_A_CHAR);     // A
    LCD_DisplayChar_8x16(2, 88, INDEX_V_CHAR);     // V
    LCD_DisplayNumber_8x16(2, 96, data->batteryCapacity, 3, 0);  // 容量值
    
    // 第三行：版本:X.XX
    LCD_DisplayChar_16x16(4, 0, INDEX_HAN_BAN);    // 版
    LCD_DisplayChar_16x16(4, 16, INDEX_HAN_BEN);   // 本
    LCD_DisplayChar_8x16(4, 32, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(4, 40, data->versionMajor + data->versionMinor/100.0f, 1, 2);  // 版本号
    
    // 第四行：通信:通/断
    LCD_DisplayChar_16x16(6, 0, INDEX_HAN_TONG);   // 通
    LCD_DisplayChar_16x16(6, 16, INDEX_HAN_XIN);   // 信
    LCD_DisplayChar_8x16(6, 32, INDEX_MAO);        // ：
    LCD_DisplayChar_16x16(6, 40, data->communicationStatus ? INDEX_HAN_TONG : INDEX_HAN_DUAN);  // 通/断
}

// 显示第三页
static void LCD_DisplayPage3(const LcdDynamicData *data) {
    LCD_Clear();
    
    // 第一行：周期:XXX UD2 U03
    LCD_DisplayChar_16x16(0, 0, INDEX_HAN_ZHOU);   // 周
    LCD_DisplayChar_16x16(0, 16, INDEX_HAN_QI);    // 期
    LCD_DisplayChar_8x16(0, 32, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(0, 40, data->cycleParam, 3, 0);  // 周期参数
    LCD_DisplayChar_8x16(0, 40 + 3*8, INDEX_U);    // U
    LCD_DisplayChar_8x16(0, 40 + 4*8, INDEX_D);    // D
    LCD_DisplayChar_8x16(0, 40 + 5*8, INDEX_NUM2); // 2
    LCD_DisplayChar_8x16(0, 40 + 6*8, INDEX_U);    // U
    LCD_DisplayChar_8x16(0, 40 + 7*8, INDEX_NUM0); // 0
    LCD_DisplayChar_8x16(0, 40 + 8*8, INDEX_NUM3); // 3
    
    // 第二行：本安输出电压:XX.XV
    LCD_DisplayChar_16x16(2, 0, INDEX_HAN_BEN_AN); // 本
    LCD_DisplayChar_16x16(2, 16, INDEX_HAN_AN);    // 安
    LCD_DisplayChar_16x16(2, 32, INDEX_HAN_SHU);   // 输
    LCD_DisplayChar_16x16(2, 48, INDEX_HAN_CHU);   // 出
    LCD_DisplayChar_16x16(2, 64, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(2, 80, INDEX_HAN_YA);    // 压
    LCD_DisplayChar_8x16(2, 96, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(2, 104, data->safetyVoltage, 2, 1);  // 电压值
    LCD_DisplayChar_8x16(2, 104 + 3*8, INDEX_V);   // V
    
    // 第三行：本安输出电流:XXXmA
    LCD_DisplayChar_16x16(4, 0, INDEX_HAN_BEN_AN); // 本
    LCD_DisplayChar_16x16(4, 16, INDEX_HAN_AN);    // 安
    LCD_DisplayChar_16x16(4, 32, INDEX_HAN_SHU);   // 输
    LCD_DisplayChar_16x16(4, 48, INDEX_HAN_CHU);   // 出
    LCD_DisplayChar_16x16(4, 64, INDEX_HAN_DIAN);  // 电
    LCD_DisplayChar_16x16(4, 80, INDEX_HAN_LIU);   // 流
    LCD_DisplayChar_8x16(4, 96, INDEX_MAO);        // ：
    LCD_DisplayNumber_8x16(4, 104, data->safetyCurrent, 3, 0);  // 电流值
    LCD_DisplayChar_8x16(4, 104 + 3*8, INDEX_m);   // m
    LCD_DisplayChar_8x16(4, 104 + 4*8, INDEX_A);   // A
}

// 显示指定页面
void LCD_ShowPage(PageIndex page, const LcdDynamicData *data) {
    switch(page) {
        case PAGE_1:
            LCD_DisplayPage1(data);
            break;
        case PAGE_2:
            LCD_DisplayPage2(data);
            break;
        case PAGE_3:
            LCD_DisplayPage3(data);
            break;
        default:
            break;
    }
}

// 更新动态数据
void LCD_UpdateDynamicData(LcdDynamicData *dest, const LcdDynamicData *src) {
    // 直接拷贝所有数据（因已保证单任务访问，无需加锁）
    *dest = *src;
}
